package cn.jimmie.learn.viewArt.weight

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.os.Build
import android.util.AttributeSet
import android.view.View
import cn.jimmie.learn.viewArt.R

/**
 * function :
 *
 * <p> Create Time: 2019年01月09日 21:36 </p>
 * <p> @author Jimmie.Qian  </p>
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class AutoLineView(context: Context, attrs: AttributeSet) : BaseView(context, attrs) {
    private val lines = arrayListOf<String>()
    private val path = Path()

    var text: String = ""
        set(value) {
            field = value
            requestLayout()
//            invalidate()
        }
    var padding: Float

    var textSize: Float = 50 * sp
        set(value) {
            field = value
            textMargin = field / 10
        }
    var color: Int
    var textMargin: Float

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.AutoLineView)
        textSize = ta.getDimension(R.styleable.AutoLineView_textSize, 20 * sp)
        padding = ta.getDimension(R.styleable.AutoLineView_android_padding, 10 * dp)
        color = ta.getColor(R.styleable.AutoLineView_textColor, Color.BLACK)
        text = ta.getText(R.styleable.AutoLineView_text) as String? ?: ""
        ta.recycle()

        textMargin = textSize / 10

        paint.textSize = textSize
        paint.color = Color.BLUE
        paint.isLinearText = true

        setLayerType(View.LAYER_TYPE_SOFTWARE, null)

        path.addArc(w / 4f, 20 * dp, w * 3f / 4f, 70 * dp, 180f, 180f)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        println("onMeasure")
        h = measureTextHeight() + (50 * dp).toInt()
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas) {
        println("onDraw")

        paint.style = Paint.Style.STROKE
        canvas.drawPath(path, paint)
        canvas.drawTextOnPath("hello android", path, 100f, 0f, paint)

        paint.style = Paint.Style.FILL
        var cursor = padding + (50 * dp)
        lines.forEach {
            cursor += textSize + textMargin
            canvas.drawText(it, padding, cursor, paint)
        }
    }

    private fun measureTextHeight(): Int {
        if (text.isEmpty()) return 0

        val maxSize = w - padding * 2
        var line = 0f
        var start = 0

        lines.clear()
        val widths = FloatArray(text.length)
        paint.getTextWidths(text, widths)
        widths.forEachIndexed { index, f ->
            line += f
            if (line > maxSize) {
                val str = text.substring(start, index)
                lines.add(str)
                line = f
                start = index
            }
        }
        // 添加尾行
        lines.add(text.substring(start, text.length))
        return (lines.size * (textSize + textMargin) + padding * 2 + textMargin).toInt()
    }

}